# Python 2.6.4
# Project Euler, Problem 55
# Copyright 2010 Talha Zaman

def rev(n): return int(str(n)[::-1])
def pal(n): return n==rev(n)
def lych(n): return n+rev(n)

ly = [0 for i in range(10000)]   #0-unchecked 1-pali 2-lych
for i in range(1,10000):
    if ly[i] != 0: continue
    count=1
    seq = [i]
    j = lych(i)
    while count<26:
        if (j<10000 and ly[j] == 1) or pal(j):
            for n in seq:
                if n<10000: ly[n] = 1
            break
        if (j<10000 and ly[j] == 2):
            for n in seq:
                if n<10000: ly[n] = 2
            break
        count += 1
        seq.append(j)
        j = lych(j)
    if count==26:
        for n in seq:
            if n<10000: ly[n] = 2
        while j<10000 and ly[j]==0:
            ly[j] = 2
            j = lych(j)
print sum([1 for x in range(10000) if ly[x]==2])
